package com.csx.micrometer.threadpool.config;

import com.csx.micrometer.common.config.MicrometerMetricProperties;
import com.csx.micrometer.threadpool.bean.ThreadPoolBeanPostProcessor;
import com.csx.micrometer.threadpool.core.ThreadPoolMetricBinder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author cuisongxu
 * @date 2022/11/20 21:13
 */
@ConditionalOnBean(ThreadPoolExecutor.class)
@EnableConfigurationProperties(MicrometerMetricProperties.class)
@Configuration
@ConditionalOnProperty(value = "micrometer.threadpool.enabled", havingValue = "true", matchIfMissing = true)
public class ThreadPoolMicroMeterAutoConfiguration {

    @Bean
    public ThreadPoolBeanPostProcessor threadPoolBeanPostProcessor() {
        return new ThreadPoolBeanPostProcessor();
    }

    @Bean
    public ThreadPoolMetricBinder threadPoolMetricBinder(Map<String, ThreadPoolExecutor> threadPoolExecutors) {
        return new ThreadPoolMetricBinder(threadPoolExecutors);
    }
}
